package com.java.shop.controller;

import com.java.shop.bean.User;
import com.java.shop.code.CommonConst;
import com.java.shop.code.StatusCode;
import com.java.shop.service.UserService;
import com.java.shop.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("")
@ResponseBody
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录验证接口
     *
     * @param userName
     * @param password
     * @return
     */
    @PostMapping(value = "/login")
    public String login(HttpServletRequest request, HttpServletResponse response, String userName, String password) {
        System.out.println(userName + "   ---   " + password);
        Map<String, Object> result = new HashMap<>();
        if ("".equals(userName) || "".equals(password)) {
            result.put("status", StatusCode.FAILURE);
            result.put("msg", "用户名和密码不能为空");
            return JSONUtil.toJson(result);
        }
        User user = userService.userLogin(userName, password);
        if (null == user) {
            result.put("status", StatusCode.FAILURE);
            result.put("msg", "用户名或密码错误");
            String data = JSONUtil.toJson(result);
            System.out.println(data);
            return data;
        }
        user.setPassword("");
        result.put("status", StatusCode.SUCCESS);
        result.put("msg", "登录成功");
        result.put("user", user);
        // 将用户信息添加到cookie和session中
        HttpSession session = request.getSession();
        session.setAttribute("username",userName);
        session.setAttribute("photo",user.getPhoto());
//        session.setMaxInactiveInterval(CommonConst.SESSION_OUT_TIME);
        session.setMaxInactiveInterval(20000);
        // 设置cookie
        Cookie cookie = new Cookie("username",userName);
        cookie.setMaxAge(-1);
        cookie.setPath("/");
        response.addCookie(cookie);
        cookie = new Cookie("photo",user.getPhoto());
        cookie.setMaxAge(-1);
        cookie.setPath("/");
        response.addCookie(cookie);
        return JSONUtil.toJson(result);
    }

    /**
     * 获取验证码
     *
     * @return
     */
    @GetMapping("/getVerifyCode")
    public String getVerifyCode() {

        return "";
    }

    /**
     * 用户注册
     *
     * @param username
     * @param password
     * @param password2
     * @param mobile
     * @return
     */
    @PostMapping("/register")
    public String register(String username, String password, String password2, String mobile) {
        System.out.println("username："+username+" -- password："+password+" -- password2："+password2+" -- mobile："+mobile);
        Map<String, Object> result = new HashMap<>();
        result.put("status", StatusCode.SUCCESS);
        if (StringUtils.isEmpty(mobile)) {
            result.put("status", StatusCode.FAILURE);
            result.put("msg", "手机号不能为空");
        }
        if (StringUtils.isEmpty(username)) {
            result.put("status", StatusCode.FAILURE);
            result.put("msg", "用户名不能为空");
        }
        if (StringUtils.isEmpty(password)) {
            result.put("status", StatusCode.FAILURE);
            result.put("msg", "密码不能为空");
        }
        if (!password.equals(password2)) {
            result.put("status", StatusCode.FAILURE);
            result.put("msg", "俩次密码不一致");
        }
        if(StatusCode.FAILURE == Integer.valueOf(result.get("status").toString())){
            return JSONUtil.mapToJson(result);
        }
        User user = userService.selectUserByMobile(mobile);
        if (null != user) {
            System.out.println("该手机号已被使用：" + user.toString());
            result.put("status", StatusCode.FAILURE);
            result.put("msg", "该手机号已被使用");
            return JSONUtil.mapToJson(result);
        }
        user = userService.selectUserByUserName(username);
        if (null != user) {
            System.out.println("该用户名已被注册：" + user.toString());
            result.put("status", StatusCode.FAILURE);
            result.put("msg", "该用户名已被注册");
            return JSONUtil.mapToJson(result);
        }
        user = new User();
        user.setUserName(username);
        user.setPassword(password);
        user.setMobile(mobile);
        user.setPhoto(CommonConst.DeFAULT_PHOTO);
        int count = userService.insertSelective(user);
        System.out.println("用户注册结果：" + count);
        if (count == 1) {
            result.put("status", StatusCode.SUCCESS);
            result.put("msg", "注册成功");
            return JSONUtil.mapToJson(result);
        }
        result.put("status", StatusCode.FAILURE);
        result.put("msg", "注册失败请联系管理员");
        return JSONUtil.mapToJson(result);
    }

}
